//选择房间号页面组件
import 'dart:math';

import 'package:flutter/material.dart';

class RoomList extends StatefulWidget {
  RoomList({super.key, required this.point, required this.building});
  //小区信息
  String point;
  //楼栋信息
  String building;

  @override
  State<RoomList> createState() => _RoomListState();
}

class _RoomListState extends State<RoomList> {
  Map roomData = {
    'point': '', //小区名称
    'building': '', //楼栋名称
    'rooms ': [], //房间号数据
  };

  @override
  void initState() {
    super.initState();
    setState(() {
      roomData['point'] = widget.point;
      roomData['building'] = widget.building;
    });
    // print({'小区信息是${roomData['point']},楼栋信息是${roomData['building']}'});
    fakeData();
  }

  //随机生成房间号
  //需求:
  // 1.随机生成3-8条数据
  // 2.随机生成1-20楼层信息
  // 3.随机生成房间号
  //房间号  楼层信息 + 0 + 房间号
  fakeData() {
    Random random = Random();
    //存储房间号数据
    List rooms = [];
    //1.随机生成3-8条数据
    int size = random.nextInt(5) + 3;
    for (int i = 0; i < size; i++) {
      //2.随机生成1-20楼层信息
      int floor = random.nextInt(20) + 1;
      //3.随机生成房间号1-4
      int room = random.nextInt(4) + 1;
      //4.拼接数据
      String roomNumber = '${floor}0$room';
      //4.1去重处理
      if (rooms.contains(roomNumber)) continue;
      //5.添加到列表中
      rooms.add(roomNumber);
    }
    setState(() {
      roomData['rooms'] = rooms;
    });
    // print(roomData['rooms']);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 设置导航条
      appBar: AppBar(
        title: Text('选择房间号'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      // 设置主体内容
      body: ListView.separated(
        padding: EdgeInsets.all(10),
        itemBuilder: (ctx, index) {
          return GestureDetector(
            onTap: () {
              Navigator.pushNamed(
                context,
                '/house_form',
                arguments: {
                  'point': roomData['point'],
                  'building': roomData['building'],
                  'room': roomData['rooms'][index],
                },
              );
            },
            child: Row(
              children: [
                //左侧文字
                Expanded(
                  child: Text(
                    '${roomData['point']}${roomData['building']}${roomData['rooms'][index]}室',
                  ),
                ),
                //右侧图标
                Icon(Icons.arrow_forward_ios, size: 20),
              ],
            ),
          );
        },
        separatorBuilder: (ctx, index) {
          return SizedBox(height: 20);
        },
        itemCount: (roomData['rooms'] as List).length,
      ),
    );
  }
}
